package cn.com.header.zbpk.web.security;

import cn.com.header.core.support.web.JsonViewData;
import cn.com.header.core.support.web.RequestUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 匿名用户访问 触发
 *
 * @author Liangzhongqiu
 * @date 2017-10-18
 * @time 21:59
 */
public class AsyncCompatibleAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

    public AsyncCompatibleAuthenticationEntryPoint(String loginFormUrl) {
        super(loginFormUrl);
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        if (RequestUtil.isAjax(request)) {
            JsonViewData.NO_LOGIN_VIEW.write(response);
            return;
        }
        super.commence(request, response, authException);
    }
}
